<?php
/**
 * 任务分类
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';

//页面显示条数
$pagesize = 10;
$page = max(1, $_GPC['page']);
$name = isset($_GPC['name']) && !empty($_GPC['name']) ? $_GPC['name'] : '';
$field = "id,pid,name,soncate,lv,sorts,isopen";
$map = 'id>0 ';
if ($name) {
    $map .= " and name like '%$name%' ";
}

if ($op == 'del') {
    if (isset($_GPC['ids'])) {
        $ids = $_GPC['ids'];
        foreach ($ids as $id) {
            $result = pdo_get('yq_haircircle_task', ['cateid' => $id]);
            if(!empty($result)){
                ajaxError('该分类下还存在有任务，不能被删除');
            }
            $res = pdo_get('yq_haircircle_task_cate', ['pid' => $id]);
            $son = pdo_get('yq_haircircle_task_cate', ['id' => $id]);
            if (!empty($res)) {
                ajaxError('请先删除子分类');
            }
            $result = pdo_delete('yq_haircircle_task_cate', array('id' => $id));
            if (!empty($result)) {
                if ($son['pid']) {
                    setSoncate($son['pid']);
                }
                ajaxError('删除成功');
            }
            ajaxError('删除失败');
        }
    }
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        $result = pdo_get('yq_haircircle_task', ['cateid' => $id]);
        if(!empty($result)){
            ajaxError('该分类下还存在有任务，不能被删除');
        }
        $res = pdo_get('yq_haircircle_task_cate', ['pid' => $id]);
        $son = pdo_get('yq_haircircle_task_cate', ['id' => $id]);
        if (!empty($res)) {
            ajaxError('请先删除子分类');
        }
        $result = pdo_delete('yq_haircircle_task_cate', array('id' => $id));
        if (!empty($result)) {
            if ($son['pid']) {
                setSoncate($son['pid']);
            }
            ajaxError('删除成功');
        }
        ajaxError('删除失败');
    }
}

if ($op == 'close') {
    if (is_ajax()) {
        $id = intval($_GPC['id']);
        $yes = intval($_GPC['yes']);
        $res = pdo_update('yq_haircircle_task_cate', array('isopen' => $yes), array('id' => $id));
        if (empty($res)) {
            ajaxError('更新失败');
        }
        ajaxSuccess('更新成功');
    }
}

if ($op == 'add') {
    if (is_ajax()) {
        error_reporting(E_ERROR);
        ini_set('display_errors', 0);
        $data['name'] = trim($_GPC['name']);
        $data['summary'] = trim($_GPC['summary']);
        $data['isopen'] = intval($_GPC['isopen']);
        $data['pid'] = intval($_GPC['pid']);
        if ($_GPC['pid']) {
            //更新父级，强制处理
            $path = setPath($data['pid']);
            $data['path'] = $path['path'];
            $data['lv'] = $path['lv'];
        } else {
            $data['path'] = 0;
            $data['lv'] = 1;
        }
        $re = pdo_insert('yq_haircircle_task_cate', $data);
        if (!empty($re)) {
            //更新父级，暂不做错误处理
            if ($data['pid']) {
                $re = setSoncate($data['pid']);
            }
            $info['code'] = 1;
            $info['msg'] = '设置成功！';
        } else {
            $info['code'] = 0;
            $info['msg'] = '设置失败！';
        }
        ajaxReturn($info);
    }
    if (isset($_GPC['id']) && $id = intval($_GPC['id'])) {
        $map .= " and (id=$id or pid=$id) ";
        $sql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
        $data = pdo_fetchall($sql);
        $father = pdo_get('yq_haircircle_task_cate', ['id' => $id], explode(',', $field));
        $father['lv'] = 0;
        $list = getTree($data, $id);
        array_unshift($list, $father);
    } else {
        $sql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
        $data = pdo_fetchall($sql);
        $list = getTree($data);
    }
    include $this->template('web/task_cate_add');
    exit;
}
if ($op == 'edit') {
    $id = intval($_GPC['id']);
    if (is_ajax()) {
//        error_reporting(E_ERROR);
//        ini_set('display_errors', 0);

        $data['name'] = trim($_GPC['name']);
        $data['summary'] = trim($_GPC['summary']);
        $data['isopen'] = intval($_GPC['isopen']);
        $re = pdo_update('yq_haircircle_task_cate', $data, array('id' => $id));
        if (!empty($re)) {
            $info['code'] = 1;
            $info['msg'] = '设置成功！';
        } else {
            $info['code'] = 0;
            $info['msg'] = '设置失败！';
        }
        ajaxReturn($info);
    }
    $map .= " and (id=$id or pid=$id) ";
    $sql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
    $data = pdo_fetchall($sql);
    $father = pdo_get('yq_haircircle_task_cate', ['id' => $id], explode(',', $field));
    $father['lv'] = 0;
    $list = getTree($data, $id);
    array_unshift($list, $father);
    include $this->template('web/task_cate_edit');
    exit;
}

$total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_task_cate') . ' WHERE ' . $map);

$sql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
$data = pdo_fetchall($sql);
$list = getTree($data);

include $this->template('web/task_cate');
exit;